/*
 * ====================================================================
 *
 * ALL RIGHTS RESERVED.
 * COPYRIGHT(C)2019,DHEE.
 */
package comm.dhee.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import comm.dhee.dao.PR20101DAO;
import comm.dhee.dao.PR20101DAOImpl;
import comm.dhee.dto.M001T001;
import comm.dhee.util.Common;


/**
 * PR201���|�o�^�Ɋւ���R���g���[���B
 * 
 * @author 鸔��g�E
 * @version $Revision: 1 $
 * 
 *          <pre>
 * ���ύX������
 * 2019/03/19 �V�K�쐬
 *          </pre>
 */
@Controller
public class PR201Controller {
	/**
	 * PR20101���|�o�^��ʂ֑J�ڂ���
	 * 
	 * @return PR20101
	 */
	@RequestMapping("toPR20101a.action")
	public String toPR20101a(HttpServletRequest request, HttpServletResponse response) {
		// PR20101DAO���C���X�^���X������i new PR20101DAOImpl() �j�G
		PR20101DAO pr20101dao = new PR20101DAOImpl();
		// rujuchuuCode��M001T001�Ƀ��b�s���O����B
		try {
			M001T001 m001t001 = pr20101dao.selkokyaku("");
			request.getSession().setAttribute("m001t001", m001t001);
			String strsumZandaka=null;
			request.getSession().setAttribute("strsumZandaka", strsumZandaka);
			String strSumuriage=null;
			request.getSession().setAttribute("strSumuriage", strSumuriage);
			String strUrikakeKinZandaka=null;
			request.getSession().setAttribute("strUrikakeKinZandaka", strUrikakeKinZandaka);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
					
		String readonly = null;
		String disabled = null;
		String disabled1 = null;
		// �󒍃R�[�h�e�L�X�g�{�b�N�X��L���ɐݒ肳���
		readonly = "";
		request.getSession().setAttribute("readonly", readonly);
		// �����{�^����L���ɐݒ肳���
		disabled = "";
		request.getSession().setAttribute("disabled", disabled);
		// �o�^�{�^���͖����ɐݒ肳���B
		disabled1 = "disabled";
		request.getSession().setAttribute("disabled1", disabled1);
		// PR20101��ʂ�߂�
		return "PR20101";
	}

	/**
	 * �����{�^���������ƁA�󒍃R�[�h�ɂ���āA�ڋq�R�[�h�A�ڋq���A���|������ʂɕ\������
	 * 
	 * @return PR20101
	 */
	@RequestMapping("toPR20101.action")
	public String check(HttpServletRequest request, HttpServletResponse response) {
		Common ex = new Common();
		int y = 0;
		String readonly = null;
		String disabled = null;
		String disabled1 = null;

		// PR20101DAO���C���X�^���X������i new PR20101DAOImpl() �j�G
		PR20101DAO pr20101dao = new PR20101DAOImpl();

		// ���͎󒍃R�[�h���擾����
		String rujuchuuCode = request.getParameter("rujuchuuCode");
		request.getSession().setAttribute("aa", rujuchuuCode);
		String message = null;
		try {

			// rujuchuuCode��M001T001�Ƀ��b�s���O����B
			M001T001 m001t001 = pr20101dao.selkokyaku(rujuchuuCode);		
			if (m001t001 == null) {
				// message:�Y������󒍃R�[�h������܂���B
				message = "alert('" + Common.M201001E + "')";
				request.setAttribute("message", message);
				// PR20101��ʂ�߂�
				return "PR20101";
			} else if (m001t001.getNounyuuHiduke() != null) {
				y = 1;
				request.getSession().setAttribute("y", y);
				
				// message:�Y�����|�����łɓo�^���܂����B
				message = "alert('" + Common.M201002E + "')";
				request.setAttribute("message", message);
				// PR20101��ʂ�߂�
				return "PR20101";
			} else {
				y = 0;
				request.getSession().setAttribute("y", y);
				// �X�V�̔��|���c���̒l
				Integer intUrikakeKinZandaka=(int) m001t001.getUrikakeKinZandaka();
				String strUrikakeKinZandaka= ex.zifuchuan(intUrikakeKinZandaka.toString());
				request.getSession().setAttribute("strUrikakeKinZandaka", strUrikakeKinZandaka);
				request.getSession().setAttribute("m001t001", m001t001);
				// ���|�����v�Z���܂��B
				M001T001 m001t002 = pr20101dao.salSumuriage(rujuchuuCode);
				// ���|���𕶎���ɕϊ�����
				double Sumuriage = m001t002.getSumuriage() * 0.8;
				request.getSession().setAttribute("doubleSumuriage", Sumuriage);
				Integer intSumuriage = (int) Sumuriage;
				String strSumuriage= ex.zifuchuan(intSumuriage.toString());
				request.getSession().setAttribute("strSumuriage", strSumuriage);
				request.getSession().setAttribute("m001t001", m001t001);
				// �󒍃R�[�h�e�L�X�g�{�b�N�X�𖳌��ɐݒ肳���
				readonly = "readonly";
				request.getSession().setAttribute("readonly", readonly);
				// �����{�^���𖳌��ɐݒ肳���
				disabled = "disabled";
				request.getSession().setAttribute("disabled", disabled);
				// �o�^�{�^���͗L���ɐݒ肳���B
				disabled1 = "";
				request.getSession().setAttribute("disabled1", disabled1);
				// PR20101��ʂ�߂�
				return "PR20101";
			}

		} catch (SQLException e) {

			e.printStackTrace();
			// PR20101��ʂ�߂�
			return "PR20101";
		}
	}

	/**
	 * �o�^�{�^���������ƁA���|���c���̒l���X�V����B
	 * 
	 * @param upurikakeKin
	 * 
	 * @return PR20101
	 */
	@RequestMapping("toPR201.action")
	public String Login(HttpServletRequest request, HttpServletResponse response) {
		Common ex = new Common();

		int y = (int) request.getSession().getAttribute("y");
		// �o�^�{�^���𖳌��ɁA�󒍃R�[�h�ƌ����C�[�����t ��L���ɐݒ肷��B
		String readonly = "";
		String disabled = null;
		String disabled1 = null;
		// �o�^�{�^���𖳌��ɐݒ肷��B
		disabled1 = "disabled";
		request.getSession().setAttribute("disabled1", disabled1);
		// �����{�^����L���ɐݒ肷��B
		disabled = "";
		request.getSession().setAttribute("disabled", disabled);
		// �󒍃R�[�h�e�L�X�g�{�b�N�X��L���ɐݒ肳���
		request.getSession().setAttribute("readonly", readonly);
		try {

			M001T001 m001t001 = (M001T001) request.getSession().getAttribute("m001t001");

			// PR20101DAO���C���X�^���X������i new PR20101DAOImpl() �j�G
			PR20101DAO pr20101dao = new PR20101DAOImpl();

			// ���͎󒍃R�[�h���擾����
			String rujuchuuCode = (String) request.getSession().getAttribute("aa");

			// ���͓��t���擾����
			String riqi = request.getParameter("riqi");

			// �V�X�e�����t���擾����
			Date day = new Date();

			// �V�X�e�����t�𕶎���ɕϊ�����
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date day1 = null;
			String message = null;
			// �ȗ����鎞�A��������ɖ{����ݒ肷��B
			if ("".equals(riqi)) {
				riqi = df.format(day);
			}
			day1 = df.parse(riqi);

			
			// �[�����t �ƃV�X�e�����t����r����B
			if (day.compareTo(day1) > 0) {

				// �[�����t�̒l���X�V���܂����B
				boolean result = pr20101dao.upNounyuuHiduke(rujuchuuCode, riqi);

				// ���|���̒l���X�V���܂����B

				boolean r = pr20101dao.upUrikakeKinZandaka(m001t001.getKokyakuCode(),(double)request.getSession().getAttribute("doubleSumuriage"));
				// �o�^�㔄�|���c���̒l
				double sumZandaka =pr20101dao.salUrikakeKinZandaka(rujuchuuCode);
				// ��L�f�[�^�X�V������ɏ��������
				if (result && r && y == 0) {
					// message�� M201004E�F���|�o�^�����B
					message = "alert('" + Common.M201004E + "')";
					request.setAttribute("message", message);
					// ���|�c���𕶎���ɕϊ�����				
					Integer intZandaka = (int) sumZandaka;
					String strsumZandaka = ex.zifuchuan(intZandaka.toString());
					request.getSession().setAttribute("strsumZandaka", strsumZandaka);
					return "PR20101";

				} else {
					// message�͂r�p�k���s��O�������܂���
					message = "alert('" + Common.ME + "')";
					request.setAttribute("message", message);
					request.getSession().setAttribute("m001t001", m001t001);
					// PR20101��ʂ�߂�
					return "PR20101";
				}
			} else {
				// ��ʂɁu M201003E�F �󒍔[�����t���s���ł��B �v�̃��b�Z�[�W��\������
				message = "alert('" + Common.M201003E + "')";
				request.setAttribute("message", message);
				request.getSession().setAttribute("m001t001", m001t001);
				// PR20101��ʂ�߂�
				return "PR20101";
			}
		} catch (Exception e1) {

			e1.printStackTrace();

		}
//		 PR20101��ʂ�߂�
		return "PR20101";
	}
}
